package com.yf.exam.modules.trainingarchives.headteacherlog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.trainingarchives.headteacherlog.dto.req.HeadTeacherLogReqDTO;
import com.yf.exam.modules.trainingarchives.headteacherlog.dto.resp.HeadTeacherLogRespDTO;
import com.yf.exam.modules.trainingarchives.headteacherlog.entity.HeadTeacherLog;
import com.yf.exam.modules.trainingarchives.headteacherlog.mapper.HeadTeacherLogMapper;
import com.yf.exam.modules.trainingarchives.headteacherlog.service.IHeadTeacherLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.TrainingClassInfoBzrExportDTO;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.three.HeadTeacherLogListExportDTO;
import com.yf.exam.modules.trainingarchives.trainingclassinfo.entity.TrainingClassInfo;
import com.yf.exam.modules.trainingarchives.trainingclassinfo.service.ITrainingClassInfoService;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.dto.resp.TrainingClassScheduleRespDTO;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.entity.TrainingClassSchedule;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 班主任工作日志表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Service
public class HeadTeacherLogServiceImpl extends ServiceImpl<HeadTeacherLogMapper, HeadTeacherLog> implements IHeadTeacherLogService {


    @Resource
    private ITrainingClassInfoService iTrainingClassInfoService;

    /**
     * 新增班主任工作日志
     * @param reqDTO
     * @return
     */
    @Override
    public Boolean add(HeadTeacherLogReqDTO reqDTO) {
        HeadTeacherLog headTeacherLog = new HeadTeacherLog();
        BeanUtils.copyProperties(reqDTO, headTeacherLog);
        headTeacherLog.setDate(LocalDate.parse(reqDTO.getDate()));
        headTeacherLog.setTime(LocalTime.parse(reqDTO.getTime()));
        return this.save(headTeacherLog);
    }

    /**
     * 删除班主任日志
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(String id) {
        LambdaQueryWrapper<HeadTeacherLog> lambdaQueryWrapper = new LambdaQueryWrapper<HeadTeacherLog>();
        lambdaQueryWrapper.eq(HeadTeacherLog::getId, id);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 更新班主任工作日志
     * @param reqDTO
     * @return
     */
    @Override
    public Boolean updateHeadTeacherLog(HeadTeacherLogReqDTO reqDTO) {
        HeadTeacherLog headTeacherLog = new HeadTeacherLog();
        BeanUtils.copyProperties(reqDTO, headTeacherLog);
        headTeacherLog.setDate(LocalDate.parse(reqDTO.getDate()));
        headTeacherLog.setTime(LocalTime.parse(reqDTO.getTime()));
        return this.updateById(headTeacherLog);
    }

    /**
     * 班主任日志列表查询
     * @param reqDTO
     * @return
     */
    @Override
    public IPage<HeadTeacherLogRespDTO> paging(PagingReqDTO<HeadTeacherLogReqDTO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(),reqDTO.getParams());
    }

    /**
     * 查看班主任日志详情
     * @param baseIdReqDTO
     * @return
     */
    @Override
    public HeadTeacherLogRespDTO getDetail(BaseIdReqDTO baseIdReqDTO) {
        HeadTeacherLogRespDTO headTeacherLogRespDTO = new HeadTeacherLogRespDTO();
        HeadTeacherLog headTeacherLog = this.getById(baseIdReqDTO.getId());
        BeanUtils.copyProperties(headTeacherLog,headTeacherLogRespDTO);
        return headTeacherLogRespDTO;
    }

    /**
     * 获取班主任日志表数据
     *
     * @param trainingClassInfoId
     * @return
     */
    @Override
    public TrainingClassInfoBzrExportDTO getHeadTeacherLogData(String trainingClassInfoId) {
        TrainingClassInfoBzrExportDTO trainingClassInfoBzrExportDTO = new TrainingClassInfoBzrExportDTO();
        List<HeadTeacherLogListExportDTO> headTeacherLogListExportDTOList = baseMapper.getTeacherLogExportDTOList(trainingClassInfoId);
        trainingClassInfoBzrExportDTO.setTeacherLogExportDTOList(headTeacherLogListExportDTOList);
        TrainingClassInfo one = iTrainingClassInfoService.getOne(new LambdaQueryWrapper<TrainingClassInfo>().eq(TrainingClassInfo::getId, trainingClassInfoId)
                .eq(TrainingClassInfo::getIsDeleted, 0));
        trainingClassInfoBzrExportDTO.setBzrgzrzjlOrganizationName(one.getOrganizationName());
        trainingClassInfoBzrExportDTO.setBzrgzrzjlClassName(one.getClassName());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        trainingClassInfoBzrExportDTO.setBzrgzrzjlTrainingStartDate(one.getTrainingStartDate().format(formatter));
        trainingClassInfoBzrExportDTO.setBzrgzrzjlHeadTeacherName(one.getHeadTeacherName());
        trainingClassInfoBzrExportDTO.setBzrgzrzjlHeadTeacherGender(one.getHeadTeacherGender());
        trainingClassInfoBzrExportDTO.setBzrgzrzjlHeadTeacherPhotoUrl(one.getHeadTeacherPhoto());
        return trainingClassInfoBzrExportDTO;
    }
}
